<?php

namespace app\push\controller;

use think\Controller;
use think\Db;

class Login extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        // if (session('userinfo.id') || session("?userinfo")) {
        //     $this->redirect("index/index");
        // }
    }

    public function login()
    {
        if (request()->ispost()) {
            $data = input('post.');
            if (!$data['account']) {
                $this->error('用户名未填写');
            }
            if (!$data['password']) {
                $this->error('密码未填写');
            }
            $userinfo = db('user')->where('account',$data['account'])->find();
            if(!$userinfo){
                $this->error('用户名不存在');
            }
            if(think_decrypt($userinfo['password'],$userinfo['salt']) != $data['password']){
                $this->error('密码输入错误');
            }
            session('userinfo',$userinfo);
            $this->success('登录成功',url('index/index'));
        } else {
            return view();
        }
    }
}
